/**
 * 
 */
package org.meruvian.yama.demo.actions;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.meruvian.yama.actions.DefaultAction;
import org.meruvian.yama.actions.annotations.ActionParam;
import org.meruvian.yama.demo.entity.Person;
import org.meruvian.yama.demo.service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.Validations;

/**
 * @author Dian Aditya
 * 
 */
@Namespace("/module/person")
@Results({
		@Result(name = DefaultAction.INDEX, type = "freemarker", location = "/org/meruvian/yama/view/module/person/person-list.ftl"),
		@Result(name = DefaultAction.SUCCESS, type = "redirectAction", location = "list") })
public class PersonAction extends DefaultAction implements ServletRequestAware {

	@Autowired
	private PersonService personService;

	@ActionParam("person")
	private Person person = new Person();

	private HttpServletRequest request;

	@Actions({ @Action(""), @Action("list"), @Action("/module/person") })
	public String index() {
		int page = 0, max = 20;
		person.setFirstName(parameter.getString("firstName"));
		person.setLastName(parameter.getString("lastName"));

		try {
			page = new Integer(parameter.getString("page"));
			max = new Integer(parameter.getString("max"));
		} catch (NumberFormatException e) {
		}

		if (max > 0) {
			parameter.put("person", personService.find(person, max, page));
		}

		return INDEX;
	}

	@Action(value = "add", results = { @Result(name = INPUT, type = "freemarker", location = "/org/meruvian/yama/view/module/person/person-add.ftl") })
	@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "person.firstName", message = "First name cannot be empty", trim = true) })
	public String form() {
		if (request.getMethod().equalsIgnoreCase("POST")) {
			return add();
		}

		return INPUT;
	}

	public String add() {
		if (person.getId() == null) {
			personService.save(person);
		} else if (person.getId().trim().equalsIgnoreCase("")) {
			person.setId(null);
			personService.save(person);
		} else {
			personService.update(person);
		}

		return SUCCESS;
	}

	@Action(value = "delete/{id}")
	public String delete() {
		person = personService.findSingle(parameter.getParameterId());
		personService.delete(person);

		return SUCCESS;
	}

	@Action(value = "edit/{id}", results = { @Result(name = INPUT, type = "freemarker", location = "/org/meruvian/yama/view/module/person/person-add.ftl") })
	public String edit() {
		parameter.put("person",
				personService.findSingle(parameter.getParameterId()));

		return INPUT;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}
}
